home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / pdmenu-0.000 / pdmenu-0 / pdmenu-0.2 / pdmenu.pl < prev    next >
Encoding:
Perl Script  |  1995-12-02  |  4.9 KB  |  198 lines

  1. #!/usr/bin/perl
  2. #
  3. #Perl-based menu system
  4. #
  5. #Uses dialog for actual IO.
  6. #
  7. #Either pass it a menu file, or it will use ~/.menurc or /etc/menurc
  8. #
  9. #This program is Copyright (C) 1995 by Joey Hess, and may be freely
  10. #distributed under the terms of the GNU public license.
  11. #
  12. #Send comments, questions, and bug reports to Joey Hess <jeh22@cornell.edu>
  13. #
  14.  
  15. #You may need to change this to some other directory. It is a temporary 
  16. #directory, and all users should have write access to it.
  17. $tmp='/tmp';
  18.  
  19. #If your clear command isn't on the path, you should change this to point
  20. #to it. Or, you can change this to change the default clear command.
  21. $clearcmd='clear';
  22.  
  23. #This is displayed as the "background title" in all dialogs of Pdmenu.
  24. #Feel free to change it.
  25. $btitle='Pdmenu 0.2 by Joey Hess <jeh22@cornell.edu>';
  26.  
  27. #This is the default directory where color definition files for Pdmenu 
  28. #are located.
  29. $colordir='/usr/local/lib/pdmenu/';
  30.  
  31. #--End of user configurable section. Code follows.--#
  32.  
  33. $tmp.='/.menu-'.$env{USER}.time;
  34. $cleartext=`$clearcmd`;
  35. $keywordlist{show}=1;
  36. $keywordlist{exec}=2;
  37. $|=1;
  38.  
  39. #Interactively fill in a blank, return the value
  40. #
  41. sub InteractiveFillin { my $title=shift; my $desc=shift;
  42.   my $cmd="dialog --backtitle \"$btitle\" --title \"$title\" --inputbox \"$desc\" 8 75";
  43.   my $a=system "$cmd 2> $tmp";
  44.   open (IN,"<$tmp"); 
  45.   @ret=<IN>; 
  46.   close IN;
  47.   unlink $tmp;
  48.     return "@ret";
  49. }
  50.  
  51.  
  52. #Run a menu command
  53. #
  54. sub RunCommand { my $command_type=shift; my $command=shift; my $fields=shift;
  55.     if ($command_type eq 1) {
  56.         do ShowMenu($command);
  57.     }
  58.     elsif ($command_type eq 2) {
  59.       #process the fields
  60.       $_=$fields;
  61.     if (/i/) { #interactively fill in parts of the command line.
  62.         $command=~s#%(.*?)%(.*?)%#&InteractiveFillin($1,$2)#eg;
  63.     }
  64.         if (/c/) { print $cleartext }
  65.         #run the program.
  66.       system $command;
  67.     }
  68.     if (/p/) { #pause
  69.         print "\nHit Enter to return to Pdmenu..";
  70.         $_=<>;
  71.     }
  72. }
  73.  
  74. #Just returns the greater of the two values.
  75. #
  76. sub PumpUp { my ($a,$b)=@_;
  77.     if ($a > $b) { return $a } else { return $b }
  78. }
  79.  
  80. #Read and parse a menu file.
  81. #
  82. sub ReadMenu { my $fn=shift;
  83.   my $count=0;
  84.     my $l=0;
  85.     my $filler=0;
  86.   open (IN,"<$fn") || exit print "Unable to open $fn.\n";
  87.   while (<IN>) {
  88.         $l++;
  89.     s/\n$//;
  90.     s/\t/ /g;
  91.         tr/ / /s;
  92.         s/^ //;
  93.         s/ $//;
  94.     if ((/^#/ eq '') && ($_ ne '')) {
  95.             if (/menu:/i ne '') { #new menu def
  96.                 my ($m,$mn,$colordef,$title,$desc)=split(/:/,$_,6);
  97.                 $menu=$mn;
  98.                 $count=0;
  99.                 $menu=~tr/A-Z/a-z/;
  100.                 $menus{$menu}[0]=$title;
  101.                 $menus{$menu}[1]=$desc;
  102.                 $menus{$menu}[2]=30;
  103.                 $menus{$menu}[2]=PumpUp($menus{$menu}[2],length($title)+5);
  104.                 $menus{$menu}[2]=PumpUp($menus{$menu}[2],length($desc)+5);
  105.                 if ($colordef) { 
  106.                     if (-e $colordef) {
  107.                         $menus{$menu}[3]=$colordef;
  108.                     }
  109.                     elsif (-e $colordir.$colordef) {
  110.                         $menus{$menu}[3]=$colordir.$colordef;
  111.                     }
  112.                     else {
  113.                         exit print "Color definition file $colordef does not exist.\n";
  114.                     }
  115.                 }
  116.                 $menucount{$menu}=0;
  117.                 $filler=0;
  118.           }
  119.           else {
  120.             my ($keyword,$hotkey,$desc,$command,$fields)=split(/:/,$_,5);
  121.                 $keyword=~tr/A-Z/a-z/;
  122.                 my $k=$keywordlist{$keyword};
  123.                 unless ($k) {
  124.                   exit print "Bad keyword in line $l: \"$keyword\"\n";
  125.                 }
  126.                 if (!$hotkey) { $hotkey=++$filler }
  127.                 $menu{$menu}[0][++$count]=$k;
  128.                 $menu{$menu}[1][$count]=$hotkey;
  129.                 $menu{$menu}[2][$count]=$desc;
  130.                 $menus{$menu}[2]=PumpUp($menus{$menu}[2],length($desc)+9);
  131.                 $menu{$menu}[3][$count]=$command;
  132.                 $menu{$menu}[4][$count]=$fields;
  133.                 $menucount{$menu}=$menucount{$menu}+1;
  134.             }
  135.       }
  136.     }
  137.   close IN;
  138. }
  139.  
  140. #Display a menu and handle its output. Pass the name of the menu to show.
  141. #
  142. sub ShowMenu { my $name=shift;
  143.     my $a=0;
  144.     
  145.     if (!$menucount{$name}) { exit print "Tried to display undefined menu: \"$name\"\n"; }
  146.     #set dialog colors
  147.     #build command for dialog.
  148.     my $height=$menucount{$name}+7; #get dialog height.
  149.     if (($height <=> 22) eq 1) { $height=21 }
  150.     my $mheight=$height-7;
  151.     my $cmd="dialog --backtitle \"$btitle\" --title \"$menus{$name}[0]\" --menu \"$menus{$name}[1]\" $height $menus{$name}[2] $mheight ";
  152.   foreach (1..$menucount{$name}) { 
  153.       $cmd.="\"$menu{$name}[1][$_]\" \"$menu{$name}[2][$_]\" ";
  154.      }
  155.     while ($a eq 0) {
  156.         $ENV{DIALOGRC}=$menus{$name}[3];
  157.         $a=system "$cmd 2> $tmp";
  158.       open (IN,"<$tmp");
  159.         @ret=<IN>;
  160.         close IN;
  161.         unlink $tmp;
  162.         
  163.       if ($a eq '0') {
  164.           foreach (1..$menucount{$name}) {
  165.             if ($ret[0] eq $menu{$name}[1][$_]) {
  166.               do RunCommand($menu{$name}[0][$_],$menu{$name}[3][$_],$menu{$name}[4][$_]);
  167.             }
  168.           }
  169.       }
  170.       else {
  171.           if ($ret[0] ne '') { exit print "Dialog error!\n"; }
  172.       }
  173.     }
  174. }
  175.  
  176. $fn=shift;
  177. if ($fn) { #load a specific file.
  178.     while ($fn) {
  179.         do ReadMenu($fn);
  180.         $fn=shift;
  181.     }
  182. }
  183. else { #load up one of the default menu files
  184.   if (-e $env{$HOME}.'.pdmenurc') {
  185.         do ReadMenu($env{$HOME}.'.pdmenurc');
  186.   }
  187.   elsif (-e '/etc/pdmenurc') {
  188.       do ReadMenu('/etc/pdmenurc');
  189.   }
  190.   else {
  191.         exit print "Unable to read pdmenurc!\n";    
  192.   }
  193. }
  194.  
  195. do ShowMenu('main');
  196.  
  197. print $cleartext;    #clean up screen.
  198.